//
//  MainViewController.swift
//  bo
//
//  Created by 肖全刚 on 16/4/27.
//  Copyright © 2016年 肖全刚. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //MARK: -内部方法
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加子控件
        addchildAllController()
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //初始化发布按钮
        setupComposeButton()
    }
    
    

    //MARK: - 添加子控制器
    private func addchildAllController()
    {
        //获取文件路径
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        
        //根据文件路径创建
        let data = NSData(contentsOfFile: path)!
        
        do
        {
            let dictArr = try NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers)
            
            //[String: AnyObject] 数组   [[String: AnyObject]] 数组中都是字典
            for dict in dictArr as! [[String: AnyObject]]
            {
                
                addChildViewController(dict["vcName"] as? String, title: dict["title"] as? String, imageName: dict["imageName"] as? String)
            }
            
        }catch
        {
            addChildViewController("HomeTableViewController",title: "首页",imageName: "tabbar_home")
            
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            
            addChildViewController(NullViewController())
            
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            
            addChildViewController("MeTableViewController", title: "我", imageName: "tabbar_profile")
        }
    }
    
    /**
     添加子控制器
     - parameter childController: 控制器
     - parameter title:           控制器标题
     - parameter imageName:       tabBar图片
     */
    private func addChildViewController(childController: String?, title: String?, imageName:String?)
    {
        //动态获取命名空间
        let  nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"]
        
        guard let ns = nameSpace as? String else
        {
            XQGLog("命名空间不存在")
            return
        }
        
        guard let vcName = childController else
        {
            XQGLog("控制器名称为nil")
            return
        }
        
        /*拼接类:
        动态获取的命名空间是没有.的需要手动添加
        AnyClass本质: AnyObject.Type
        UIViewController本质: UIViewController.Type
        */
        
        let cls:AnyClass? = NSClassFromString(ns + "." + vcName)
        
        //将Any 类型转换为UIViewController 类型
        guard let clsType = cls as? UIViewController.Type else
        {
            XQGLog("传入的字符串,不能当做UIViewController来使用")
            return 
        }
        
        

         tabBar.tintColor = UIColor.orangeColor()
        
        //根据控制器类,类型创建控制器对象
        let  vc = clsType.init()
        
        vc.title = title;
        
        if  let iName = imageName
        {
            vc.tabBarItem.image = UIImage(named: iName)
            vc.tabBarItem.selectedImage = UIImage(named: iName + "_highlighted")
        }
        
                //包含一个导航控制器
        let  nav = UINavigationController(rootViewController: vc)
        //添加
        addChildViewController(nav)
    }
    
    
    //MARK: - 发布按钮懒加载
   private lazy var composeButton:UIButton =
        {
            let btn = UIButton()
            
            //背景图
            btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
            btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
            
            //默认图
            btn.setImage(UIImage(named:"tabbar_compose_icon_add" ), forState: UIControlState.Normal)
            btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
            
       
            btn.addTarget(self, action: Selector("composeBtnClick"), forControlEvents: UIControlEvents.TouchUpInside)

            print(btn.superclass)
            
            btn.sizeToFit()
            
            return btn;
        }()
    
    //计算frame
    private func setupComposeButton()
    {
        tabBar.addSubview(composeButton)
        
        let width = UIScreen.mainScreen().bounds.width / CGFloat(childViewControllers.count)
        
        let heigth = tabBar.bounds.height
        
        let rect = CGRect(origin: CGPointZero, size: CGSizeMake(width, heigth))
        
        composeButton.frame =  CGRectOffset(rect, 2 * width, 0)
    }
    
    @objc private func composeBtnClick()
    {
        XQGLog("中间按钮点击")
    }
   

}

